<?php slot('coursesNav', 'active') ?>

<script type="text/javascript" src="/js/fileupload.js"></script>
<script>

first_load = true;

	function reloadFilesList() {
		//fix to make sure the first call does not override the call to the tab content
	
		if(first_load)
		{
			first_load = false;
		}
		else
		{
	
			$.get("<?php echo url_for('library/courseDetail').'?course_id='.$course->getId()?>", {}, function(data){ $('#course_tab_content').html(data); bindPageLink();});
			$('#course-library-upload-form').hide();
			
		}		
	}

	function uploadFormRef() {
		return '#course-library-upload-form';
	}
</script>


<div id="wrapper" class="profile">
	<div id="content">
		<div id="main">
			<div class="main-heading">
				<?php //include_component('notes','filter', array("semester_id" => $semester_id)) ?>
				<h1><?php echo $course->getSubject() ? $course->getSubject()->getName(). ': ' : '' ?><?php echo $course->getName() ?></h1>
			</div>
		
			<div class="profile-nav">
				<ul class="display-options"  id="note-options" style="display:none;">
					<li><a></a><li>
				</ul>
			
				<ul class="display-options" id="library-options" style="display:none;">
					<?php if($upload_access == 'yes'):?>
						<li class="upload" >
										<a class="btn_upload" href="#">Upload</a>
										<?php include_partial('courses/libraryForm', array('course_id' => $course->getId()));?>
						</li>
					<?php endif?>
						<li class="active"><a class="btn_detail" href="<?php echo url_for('library/courseDetail').'?course_id='.$course->getId()?>">Detail view</a></li>
						<li><a class="btn_list" href="<?php echo url_for('library/courseList').'?course_id='.$course->getId()?>">List view</a></li>
											
				</ul>
				<ul class="by-section">
					<li>
					
							<a href="<?php echo url_for('courses/notesList').'?course_id='.$course->getId()?>">Notes</a>
							<div style="display:none">
							<ul>
								<li><a></a></li>
							</ul>
							</div>
					
					</li>
					<li>
					
							<a href="<?php echo url_for('library/courseDetail').'?course_id='.$course->getId()?>">Library</a>
							<div style="display:none">
								<ul>
									<li class="active"><a class="btn_detail" href="<?php echo url_for('library/courseDetail').'?course_id='.$course->getId()?>">Detail view</a></li>
									<li><a class="btn_list" href="<?php echo url_for('library/courseList').'?course_id='.$course->getId()?>">List view</a></li>
								</ul>
							</div>
									
					</li>
					
				</ul>
			</div>
			
			<div id="course_tab_content">
				Loading Content...
			</div>

		</div>
		
		<div id="sidebar">
			<div class="module study-groups" style="padding-left: 10px;">
				<h3 class="gradient light">Contribute To This Course</h3>
				<div class="inner">
					<ul>
						<li>
							<h4>
								<?php echo link_to('Post Your Course Notes', 'notes/new?course_id='.$course_id) ?>
							</h4>
						</li>
					</ul>
				</div>
			</div>
			<div style="padding-left: 10px;">
				<?php include_component('courses', 'about', array('course' => $course))?>
			</div>
			<div class="module groups" style="padding-left: 10px;">
				<h3 class="gradient dark">
					<?php echo link_to('Create a Group', 'groups/edit')?>
					Study Groups 
				</h3>
				<div class="inner">
				<?php if(isset($groups) && $groups != null): ?>
					<ul>
						<?php foreach($groups as $group): ?>
						<li>
							<h4>
								<?php echo link_to($group->getName(), 'groups/show?id='.$group->getId()) ?>
							</h4>
							<p class="activity"><span class="stars three">3 stars</span>
							<?php echo $group->countGroupMembers() ?> members</p>
							<p><?php echo $group->getDescription() ?></p>
						</li>
						<?php endforeach ?>
					</ul>
				<?php else: ?>
					<div class="empty">
						<img alt="Sample view" src="/images/img_sample_groups-sidebar.jpg"/>
						<p>There are no groups for this course</p>
					</div>
				<?php endif ?>
				</div>
			</div>
		</div>
		
		</div>
	</div>



<script>
library_view = null;
library_page = 1;

	//returns the parameter string that should be used to filter the content 
	function getFilterParams(){

		return '&page='+library_page;

	}

//bind tabs for ajax navigation
	
	
	
	/**
	*  show tab content when clicked
	*/
	function displayTabContent(element) {
		//redistribute css
		$('.by-section > li').attr('class', '');

		//change button look
		$(element).addClass('active');

		//get the href for the clicked tab
		url = $(element).children().eq(0).attr('href');
		library_view = url;
		library_page = 1;
		
		//redisplay content
		$.get( url, {},
				function(data) {

					$('#course_tab_content').html(data);
					//display option buttons of clicked tab
					$('.display-options').hide();
					$('#note-options').show();
					
					//before(($(element).children().eq(1).children().eq(0).html()));
					bindNotesPageLink();
					rebind();
					
				}
		);
	}

		function displayLibraryTabContent(element) {
			//redistribute css
			$('.by-section > li').attr('class', '');

			//change button look
			$(element).addClass('active');

			//get the href for the clicked tab
			url = $(element).children().eq(0).attr('href');
			library_view = url;
			library_page = 1;
			
			//redisplay content
			$.get( url, {},
					function(data) {

						$('#course_tab_content').html(data);
						//display option buttons of clicked tab					
						$('.display-options').hide();
						$('#library-options').show();

						//before(($(element).children().eq(1).children().eq(0).html()));
						
						rebind();
						
					}
			);
			
		}
		
	
	
	
	/**
	*  Bind tab links
	*/
	function bindTabs() {
		$('.by-section > li').eq(0).click(function()
			{
					//hide extra content generate by tabs
					displayTabContent(this);
					return false;
				
			});

		$('.by-section > li').eq(1).click(function()
				{
						//hide extra content generate by tabs
						displayLibraryTabContent(this);
						return false;
					
				});
	}


	//refresh option buttons after new data is displayed
	function bindOptionButtons()
	{

		//must make the link return false on click so that only that element is effected and not the form as well
		$('.btn_upload').each(function(){$(this).children().eq(0).click(function(){return false})});	
		
		//bind ajax request to buttons
		$('.display-options > li').each(function()
					{
						$(this).click( displayOption );
					}
		);
	} 


	//display new option
	function displayOption()
	{

		//do not bind if an upload button
		if($(this).attr('class') == 'upload')
		{
			
			 showForm();
			 return true;
		}
		else
		{

			$('.display-options > li').attr('class', '');

			$('.display-options > li').eq(0).attr('class', 'upload');
			$(this).attr('class', 'active');
		
			//get the href for the clicked tab
			url = $(this).children().eq(0).attr('href');
			library_view = url;
			
			//redisplay content
			$.get( url, {},
					function(data) {
	
						$('#course-library-upload-form').hide();
						$('#course_tab_content').html(data);					
						rebind();
					}
			
			);

			return false;
		}
	}

	
//====END TABS ======================//



	//============page filters for library
	function bindPageLink()
	{
		$('.pager > ul').children().click(pageData);
		$('.prev').click(pageData);
		$('.next').click(pageData);
	}

	function pageData()
	{	//create url to retrieve data
		library_page = parseInt($(this).children().eq(0).attr('href'));
		url = library_view + getFilterParams();
		
		//redisplay content
		$.get( url, {},
				function(data) {
			
			$('#course-library-upload-form').hide();
			$('#course_tab_content').html(data);
			rebind();						
				}
		);
		return false;		
	}


	function bindNotesPageLink()
	{
		
			$('.view-all > a').click(function(){

				url = $(this).attr('href');				
				//redisplay content
				$.get( url, {},
						function(data) {
					
					$('#course-library-upload-form').hide();
					$('#course_tab_content').html(data);
					rebind();
					bindNotesPageLink();						
						}
				
				);
				return false;		
			});
	}

	//upload form button==============
		
		//show dropdown for file upload
		function showForm(){

				$('#course-library-upload-form').show();
				
		}


		//bind form cancle button
		function bindFormCancel()
		{
			$('#upload-cancel').click(
					function()
					{
						$('#course-library-upload-form').hide();
						return false;
					}
					);
		}



		function bindFileDelete()
		{
			$('.library-file-delete').click(
					function()
					{
						
						//get the href for the clicked button
						url = $(this).attr('href');
						
						//redisplay content
						$.get( url, {},
								function(data) {
									$('.display-options > li').eq(<?php if($upload_access == 'yes'):?>1<?php else:?>0<?php endif?>).click();
								}
						);

						return false;
					}
					);
		}
		
//===global binding function to reset environment links
	function rebind()
	{
		bindFormCancel();
		bindPageLink();
		bindFileDelete();
	}

	$(document).ready(function(){	
		
		bindTabs();
		bindFormCancel();
		bindPageLink();
		bindOptionButtons();

		$('.by-section > li').eq(0).click();
		
	});
	

</script>